﻿using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Library.WebControls.SimplePaging {
	public sealed class CenterCellPaging : TableCell {
		private class FirstIcon : IconControl {
			[DefaultValue("~/style/simple/images/nav_left_end.png")]
			public override string ImageUrl {
				get {
					return base.ImageUrl;
				}
				set {
					base.ImageUrl = value;
				}
			}
		}
		private class PreviewIcon : IconControl {
			[DefaultValue("~/style/simple/images/nav_left.png")]
			public override string ImageUrl {
				get {
					return base.ImageUrl;
				}
				set {
					base.ImageUrl = value;
				}
			}
		}
		private class NextIcon : IconControl {
			[DefaultValue("~/style/simple/images/nav_right.png")]
			public override string ImageUrl {
				get {
					return base.ImageUrl;
				}
				set {
					base.ImageUrl = value;
				}
			}
		}
		private class LastIcon : IconControl {
			[DefaultValue("~/style/simple/images/nav_right_end.png")]
			public override string ImageUrl {
				get {
					return base.ImageUrl;
				}
				set {
					base.ImageUrl = value;
				}
			}
		}
		public CenterCellPaging() {
			CssClass = "center-cell-paging";
			HorizontalAlign = HorizontalAlign.Center;
		}
		[DefaultValue("center-cell-paging")]
		public override string CssClass {
			get {
				return base.CssClass;
			}
			set {
				base.CssClass = value;
			}
		}
		[DefaultValue(HorizontalAlign.Center)]
		public override HorizontalAlign HorizontalAlign {
			get {
				return base.HorizontalAlign;
			}
			set {
				base.HorizontalAlign = value;
			}
		}

		protected override void CreateChildControls() {
			_centerTable = new Table();
			_centerTable.CellPadding = 0;
			_centerTable.CellSpacing = 0;
			_centerTable.Rows.Add(new TableRow());
			Controls.Add(_centerTable);
			_first = new FirstIcon() { ImageUrl = "~/style/simple/images/nav_left_end.png" };
			AddControl(_first, "sppaging-icon");

			_preview = new PreviewIcon() { ImageUrl = "~/style/simple/images/nav_left.png" };
			AddControl(_preview, "sppaging-icon");

			//_leftSpliter = new HtmlGenericControl("span");
			//_leftSpliter.Attributes["class"] = "sppaging-spliter";
			//Controls.Add((_leftSpliter));
			_leftSpliter = AddSpliter();

			_pageControl = new Label();
			AddControl(_pageControl, "sppaging-text");
			//if (CreateCurrentPageTextBox)
			//if (CreateCurrentPageText != null) {
			//	_currentPageText = CreateCurrentPageText();
			//} else {
			_currentPageText = new TextBox();// { Text = "0", Width = new Unit("50px") };
			//}
			//_currentPageText.ID = "pageIndex";
			_currentPageText.Text = "0";
			_currentPageText.Width = new Unit("50px");
			_currentPageText.CssClass = "current-page-index";

			//_currentPageText.ID = "pageIndex";
			AddControl(_currentPageText, "sppaging-input-text");

			_leftOfControl = new OfLable { Text = "Of" };
			AddControl(_leftOfControl, "sppaging-text");
			//Controls.Add(CreateSpaceText());

			_totalPages = new ZeroLable { Text = "0", FirstSpace = true };

			AddControl(_totalPages, "sppaging-text");



			_maxPages = 0;

			//_rightSpliter = new HtmlGenericControl("span");
			//_rightSpliter.Attributes["class"] = "sppaging-spliter";
			//Controls.Add(_rightSpliter);
			_rightSpliter = AddSpliter();
			_next = new NextIcon() { ImageUrl = "~/style/simple/images/nav_right.png" };
			AddControl(_next, "sppaging-icon");



			_last = new LastIcon() {
				ImageUrl = "~/style/simple/images/nav_right_end.png"
			};
			AddControl(_last, "sppaging-icon");


			_numberOfRowDisplayControl = new Label();
			//_numberOfRowDisplayControl.Text = "Row per page";
			AddControl(_numberOfRowDisplayControl, "sppaging-text");

			//if (CreateNumberRowOfTextBox != null)
			//	_numberRowOfText = CreateNumberRowOfTextBox();
			//else
			_numberRowOfText = new TextBox();
			//_numberRowOfText.ID = "pageSize";
			_numberRowOfText.Text = "0";
			_numberRowOfText.Width = new Unit("30px");
			_numberRowOfText.CssClass = "current-page-size";
			AddControl(_numberRowOfText, "sppaging-input-text");

			//Controls.Add(_first);

		}

		private static HtmlGenericControl CreateSpaceText() {
			return new HtmlGenericControl("span") { InnerText = " " };
		}
		private SpliterControl AddSpliter() {
			var span = new SpliterControl("div");
			//span.Attributes["class"] = clazz;

			//var div = new HtmlGenericControl("div");
			//span.Controls.Add(div);
			//div.Attributes["class"] = clazz;
			//div.Controls.Add(ctr);
			var cell = new TableCell();
			cell.Controls.Add(span);
			_centerTable.Rows[0].Cells.Add(cell);
			return span;
		}
		private void AddControl(Control ctr, string clazz) {
			var cell = new TableCell { VerticalAlign = VerticalAlign.Middle };
			var div = new HtmlGenericControl("div");
			div.Attributes["class"] = clazz;
			//span.Attributes["class"] = clazz;
			//if (ctr != null)
			cell.Controls.Add(div);
			div.Controls.Add(ctr);
			//var div = new HtmlGenericControl("div");
			//span.Controls.Add(div);
			//div.Attributes["class"] = clazz;
			//div.Controls.Add(ctr);
			//  var cell = new TableCell();
			//cell.Controls.Add(span);
			_centerTable.Rows[0].Cells.Add(cell);
			//return span;

			//Controls.Add(span);
		}

		private Table _centerTable;

		//private HtmlGenericControl _spanFirst;
		private Image _first;
		//private HtmlGenericControl _spanPreview;
		private Image _preview;
		private SpliterControl _leftSpliter;
		private Label _pageControl;
		private TextBox _currentPageText;
		private Label _leftOfControl;
		private int _maxPages;
		private SpliterControl _rightSpliter;

		//private HtmlGenericControl _spanNext;
		private Image _next;
		//	private HtmlGenericControl _spanLast;
		private Image _last;
		private Label _numberOfRowDisplayControl;
		private TextBox _numberRowOfText;

		private Label _totalPages;
		public Label TotalPagesText {
			get { EnsureChildControls(); return _totalPages; }
		}
		public Image First {
			get { EnsureChildControls(); return _first; }
		}

		public Image Preview {
			get { EnsureChildControls(); return _preview; }
		}

		public SpliterControl LeftSpliter {
			get { EnsureChildControls(); return _leftSpliter; }
		}

		public Label PageControl {
			get { EnsureChildControls(); return _pageControl; }
		}

		internal TextBox CurrentPageText {
			get { EnsureChildControls(); return _currentPageText; }
		}

		public Label LeftOfControl {
			get { EnsureChildControls(); return _leftOfControl; }
		}
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Browsable(false)]
		[DefaultValue(0)]
		public int MaxPages {
			get { return _maxPages; }
		}

		public SpliterControl RightSpliter {
			get { EnsureChildControls(); return _rightSpliter; }
		}

		public Image Next {
			get { EnsureChildControls(); return _next; }
		}

		public Image Last {
			get { EnsureChildControls(); return _last; }
		}

		public Label NumberOfRowDisplayControl {
			get { EnsureChildControls(); return _numberOfRowDisplayControl; }
		}

		internal TextBox NumberRowOfText {
			get { EnsureChildControls(); return _numberRowOfText; }
		}

		//protected override void Render(HtmlTextWriter writer)
		//{
		//  _currentPageText.ID = "crtPid";
		//  _numberRowOfText.ID = "numb";
		//  base.Render(writer);
		//}


		//internal delegate TextBox CreateCurrentPageTextBoxHandler();

		//internal delegate TextBox CreateNumberRowOfTextBoxHandler();

		//internal CreateNumberRowOfTextBoxHandler CreateNumberRowOfTextBox;
		//internal CreateCurrentPageTextBoxHandler CreateCurrentPageText;

		private class SpaceFirstLabel : Label {
			protected override void Render(HtmlTextWriter writer) {
				//LiteralControl space = new LiteralControl(" ");
				//space.RenderControl(writer);
				writer.Write("&nbsp;");
				//writer.Write("t"););
				base.Render(writer);
			}
		}
		private class OfLable : System.Web.UI.WebControls.Label {
			[DefaultValue("Of")]
			public override string Text {
				get {
					return base.Text;
				}
				set {
					base.Text = value;
				}
			}
		}




	}
}
